package utils;

/**
 * PriceUtility menyediakan method untuk konversi koin ke harga dan harga ke
 * koin
 *
 * @author Muhammad Anis
 */
public class PriceUtility {

    public static final int DIME_VALUE = 10;
    public static final int NICKEL_VALUE = 5;
    public static final int QUARTER_VALUE = 25;

    public static Triplet<Integer, Integer, Integer> PriceToCoin(int dimeCount, int nickelCount, int quarterCount, int price) {
        Triplet<Integer, Integer, Integer> retVal = new Triplet<>(0, 0, 0);
        int quarter, dime, nickel;
        quarter = price / QUARTER_VALUE;
        if (quarter > quarterCount) {
            quarter = quarterCount;
        }
        price -= (quarter * QUARTER_VALUE);
        //quarter done
        retVal.setFirst(quarter);
        //cek untuk dime
        if (price > 0) {
            dime = price / DIME_VALUE;
            if (dime > dimeCount) {
                dime = dimeCount;
            }
            price -= (dime * DIME_VALUE);
            retVal.setSecond(dime);
            if (price > 0) {
                nickel = price / NICKEL_VALUE;
                if (nickel > nickelCount) {
                    nickel = nickelCount;
                }
                price -= nickel * NICKEL_VALUE;
                retVal.setThird(nickel);
            }
        }

        if (price != 0) {
            return null;
        }
        return retVal;
    }

    public static int CoinToPrice(int nickelCount, int dimeCount, int quarterCount) {
        int total = nickelCount * NICKEL_VALUE + dimeCount * DIME_VALUE + quarterCount * QUARTER_VALUE;
        System.out.println("jumlah koin: " + total);
        return total;
    }
}
